package com.docshell.service;

import com.docshell.common.DbManager;
import com.docshell.common.DisplayManager;
import com.docshell.constants.DocShellConstants;
import com.docshell.exceptions.DocShellException;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.common.DfException;

public class QueryHandler {

	private String CurrentFolderPath = null;
	private String FolderPath = null;
	private String ObjectPattern = null;
	
	private boolean longList = false;

	public QueryHandler(String currentFolderPath) {
		CurrentFolderPath = currentFolderPath;
	}
	
	public QueryHandler() {
	}

	public void setFolderPath(String folderPath) {
		FolderPath = folderPath;
	}

	public String getFolderPath() {
		return FolderPath;
	}

	public void setLongList(boolean longList) {
		this.longList = longList;
	}

	public boolean getLongList() {
		return longList;
	}

	public void displayResult() throws DfException, DocShellException {
		try {
			String lsQuery = "";
			lsQuery = "SELECT ";
			if (getLongList()) {
				lsQuery = lsQuery + DocShellConstants.LONG_COLUMN_LIST;
			}
			else
			{
				lsQuery = lsQuery + DocShellConstants.SHORT_COLUMN_LIST;
			}
			lsQuery = lsQuery + " FROM dm_sysobject ";
			lsQuery = lsQuery + " WHERE FOLDER('" + getFolderPath() + "')";
			if ((null!=getObjectPattern()) && (!getObjectPattern().equals(DocShellConstants.EMPTY_STRING))) {
				lsQuery = lsQuery + " UNION SELECT ";
				if (getLongList()) {
					lsQuery = lsQuery + DocShellConstants.LONG_COLUMN_LIST;
				}
				else
				{
					lsQuery = lsQuery + DocShellConstants.SHORT_COLUMN_LIST;
				}
				lsQuery = lsQuery + " FROM dm_sysobject ";
				lsQuery = lsQuery + " WHERE FOLDER('" + CurrentFolderPath + "')";
				lsQuery = lsQuery + " AND object_name LIKE '%" + getObjectPattern() + "%'";
			}
			IDfCollection dfColl = DbManager.getData(lsQuery);
			DisplayManager.displayCollectionResult(dfColl);
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.toString());
		}
	}

	public void readQuery(String strQuery) throws DfException, DocShellException{
		try {
			IDfCollection dfColl = DbManager.getData(strQuery);
			DisplayManager.displayCollectionResult(dfColl);
		} catch (DfException dfe) {
			throw dfe;
		} catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.toString());
		}
	}

	public void execQuery(String strQuery) throws DfException, DocShellException{
		try {
			DbManager.execQuery(strQuery);
		} catch (DfException dfe) {
			throw dfe;
		}
		catch (Exception ex) {
			throw new DocShellException(DocShellException.UNKNOWN_EXCEPTION, ex.toString());
		}
	}
		
	
	public void setObjectPattern(String objectPattern) {
		ObjectPattern = objectPattern;
	}

	public String getObjectPattern() {
		return ObjectPattern;
	}
	

}
